package com.douya.pangu.application.basic.mapper;

import com.douya.pangu.application.basic.pojo.entity.CoreUserDO;
import com.github.yulichang.base.MPJBaseMapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 说明：核心用户表Mapper
 *
 * @author hcy
 * @date 2022/11/28
 */
@Mapper
public interface CoreUserMapper extends MPJBaseMapper<CoreUserDO>, BaseMapper<CoreUserDO> {

    /**
     * 根据用户id获取用户角色权限
     *
     * @param id 用户id
     * @return 权限列表
     */
    @Select("select pcp.permission_code " +
            "from pg_core_user pcu " +
            "         left join " +
            "     pg_core_user_role pcur on pcu.id = pcur.user_id " +
            "         left join " +
            "     pg_core_role pcr on pcur.role_id = pcr.id " +
            "         left join " +
            "     pg_core_role_permission pcrp on pcur.role_id = pcrp.role_id " +
            "         left join " +
            "     pg_core_permission pcp on pcrp.permission_id = pcp.id " +
            "where pcu.id = #{id} and pcu.is_deleted = 0 " +
            "  and pcr.is_deleted = 0 " +
            "  and pcp.is_deleted = 0 ")
    List<String> rolePermissionCodeByUserId(Long id);

    /**
     * 根据用户id获取用户权限
     *
     * @param id 用户id
     * @return 权限列表
     */
    @Select("select pcp.permission_code  " +
            "from pg_core_user pcu  " +
            "         left join pg_core_user_permission pcup on pcu.id = pcup.user_id  " +
            "         left join pg_core_permission pcp on pcup.permission_id = pcp.id  " +
            "where pcu.id = #{id} and pcu.is_deleted = 0  " +
            "  and pcp.is_deleted = 0 ")
    List<String> permissionCodeByUserId(Long id);
}